/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-26
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.order.entity.*;
import com.rzico.order.service.EmptyBarrelService;
import com.rzico.order.service.ReturnService;
import com.rzico.order.service.ShippingService;
import com.rzico.order.service.VirtualStockService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 空桶记录表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-07-26
 */
@Api(description = "空桶记录表接口")
@RestController
@RequestMapping("/admin/emptyBarrel")
public class EmptyBarrelAdminController extends BaseController {

    @Autowired
    private EmptyBarrelService emptyBarrelService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private VirtualStockService virtualStockService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 分页查询空桶记录表
     *
     * @return
    */
    @ApiOperation("分页查询空桶记录表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "audited", value = "是否审核(0.待确认,1.已确认)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shippingEmployeeId", value = "送货员ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "emptyType", value = "记录类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> list(Date startDate, Date endDate,Long memberId,Integer audited,Long productId,Long shopId,Integer emptyType,String shippingEmployeeId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (memberId!=null){
            params.put("memberId", memberId);
        }

        if (audited!=null){
            params.put("audited", audited);
        }

        if (shopId!=null){
            params.put("shopId", shopId);
        }


        if (productId!=null){
            params.put("productId", productId);
        }


        if (emptyType!=null){
            params.put("emptyType", emptyType);
        }

        if (shippingEmployeeId!=null){
            params.put("shippingEmployeeId", shippingEmployeeId);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EmptyBarrel> list = emptyBarrelService.selectList(params);
        PageResult<EmptyBarrel> pageResult = new PageResult<EmptyBarrel>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }



    /**
     * 分页查询空桶库存表
     *
     * @return
     */
    @ApiOperation("分页查询空桶库存表")
    @GetMapping("/summaryList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> summaryList(Date startDate, Date endDate,Long shopId,Long productId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", startDate);
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (shopId!=null){
            params.put("shopId", shopId);
        }


        if (productId!=null){
            params.put("productId", productId);
        }

        params.put("audited", true);


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EmptyBarrel> list = emptyBarrelService.selectList(params);
        PageResult<EmptyBarrel> pageResult = new PageResult<EmptyBarrel>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 存桶登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("存桶登记")
    @PostMapping("/addEmptyBarrel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "仓库ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shippingEmployeeId", value = "送货员ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> addEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList,Long shopId,String shippingEmployeeId) {

        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.addEmptyBarrel(emptyItemList,shopId,shippingEmployeeId);

        return CommResult.success();

    }

    /**
     * 欠桶登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("欠桶登记")
    @PostMapping("/decEmptyBarrel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "仓库ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shippingEmployeeId", value = "送货员ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> decEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList,Long shopId,String shippingEmployeeId) {
        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.decEmptyBarrel(emptyItemList,shopId,shippingEmployeeId);

        return CommResult.success();

    }

    /**
     * 入库登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("入库登记")
    @PostMapping("/addStock")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "仓库ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> addEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList,Long shopId) {

        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.addStock(emptyItemList,shopId);

        return CommResult.success();

    }

    /**
     * 出库登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("出库登记")
    @PostMapping("/decStock")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "仓库ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> decEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList,Long shopId) {
        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.decStock(emptyItemList,shopId);

        return CommResult.success();

    }

    /**
     * 空桶压金汇总
     *
     * @return
     */
    @ApiOperation("空桶压金汇总")
    @PostMapping("/summary")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query")
    })
    public CommResult<Order> summary(Long memberId) {

        Enterprise enterprise = enterpriseService.getCurrent();

        Map<String,Object> data = new HashMap<>();

        BigDecimal emptyBarrel = emptyBarrelService.selectQuantitySum(memberId);
        BigDecimal stock = virtualStockService.selectStockSum(enterprise.getId(),memberId,3);

        //合计余桶
        data.put("emptyBarrel",emptyBarrel);

        //合计压金
        data.put("price",virtualStockService.selectPriceSum(enterprise.getId(),memberId,3));

        //合计压桶
        data.put("stock",stock);

        BigDecimal balance = emptyBarrel.subtract(stock);

        if (balance.compareTo(BigDecimal.ZERO)>0) {
            //存桶数
            data.put("emptyBarrelMany", 0);
            //欠桶数
            data.put("emptyBarrelLess", balance);
        } else {
            //存桶数
            data.put("emptyBarrelMany", BigDecimal.ZERO.subtract(balance));
            //欠桶数
            data.put("emptyBarrelLess", 0);
        }


        return CommResult.success(data);

    }

    /**
     * 回桶确认
     *
     * @return
     */
    @ApiOperation("回桶确认")
    @PostMapping("/audited")

    public CommResult<Order> audited(@RequestBody List<EmptyBarrel> emptyItemList) {

        emptyBarrelService.audited(emptyItemList);

        return CommResult.success();

    }


}
